package com.manage.controller; /**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/27 12:59
 */

import com.manage.dao.ProfileDaoImpl;
import com.manage.pojo.Page;
import com.manage.pojo.Profile;
import com.manage.service.ProfileService;
import com.manage.service.ProfileServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ListServlet", value = "/list")
public class ListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.查询所有档案信息
        ProfileService service = new ProfileServiceImpl(new ProfileDaoImpl());

        //2.获取当前需要调整到的页数
        String pageIndex = request.getParameter("pageIndex");
        //如果当前页等于null,那么让它跳转到第一页
        if (pageIndex == null) {
            pageIndex="1";
        }
        //2.封装Page分页对象, 第一个参数:当前需要跳转到的页码, 第二个参数:每页显示条数
        Page page = new Page(Integer.parseInt(pageIndex), 2);
        //分页查询
        List<Profile> list =  service.findAllPage(page);
        //查询一共有多少条数据
        int count = service.findAllPageCount();
        //将查询到的总条数, 存入page分页对象中
        page.setTotalCounts(count);

        /**
         * 使用jsp来展示书籍列表
         * 将数据保存到request域中
         * */
        request.setAttribute("list",list);
        //将分页对象传给页面展示
        request.setAttribute("page",page);
        //将当前页返回给页面
        request.setAttribute("pageIndex",pageIndex);
        //页面跳转
        request.getRequestDispatcher("/list.jsp").forward(request,response);

    }
}
